package com.liholin.springboot.sample.service.impl;

import com.liholin.springboot.sample.service.UploadFileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UploadFileServiceImpl implements UploadFileService {

    private String dateFolderFormat = "yyyy-MM-dd";
    private String uploadImageFolder = "/user/mindy/Desktop/";

    @Override
    public List multipleImageUpload(List<MultipartFile> multipartFiles) throws IOException {
        List urlList = new ArrayList();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFolderFormat);
        Date now = new Date();
        String fileTime = simpleDateFormat.format(now);
        for(MultipartFile multipartFile:multipartFiles){
            //获取文件名
            String fileName = multipartFile.getOriginalFilename();
            //获取文件后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            String filePath = uploadImageFolder + "/" + fileTime + "/" + UUID.randomUUID().toString().replace("-", "").toLowerCase() + suffixName;
//            InputStream in = multipartFile.getInputStream();
//            AliyunOperation.uploadFile(in, OSSENDPOINT, OSSKEYID, OSSKEYSECRET, OSSBURCKET, filePath);
            urlList.add(/*OSSREADURL + "/" +*/ filePath);
        }
        return urlList;
    }
}
